package com.dayuanit.dy15.atm.service.impl;

import com.dayuanit.dy15.atm.domain.CardFlowDomain;
import com.dayuanit.dy15.atm.domain.FlowDomain;
import com.dayuanit.dy15.atm.dto.CardFlowStatisticsDTO;
import com.dayuanit.dy15.atm.dto.FlowDTO;
import com.dayuanit.dy15.atm.entity.Card;
import com.dayuanit.dy15.atm.entity.Flow;
import com.dayuanit.dy15.atm.exception.BizException;
import com.dayuanit.dy15.atm.holder.PageHolder;
import com.dayuanit.dy15.atm.mapper.CardMapper;
import com.dayuanit.dy15.atm.mapper.FlowMapper;
import com.dayuanit.dy15.atm.service.FlowService;
import com.dayuanit.dy15.atm.util.CardUtil;
import com.dayuanit.dy15.atm.util.DateUtil;
import com.dayuanit.dy15.atm.util.MoneyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class FlowServiceImpl implements FlowService {

    @Autowired
    private FlowMapper flowMapper;

    @Autowired
    private CardMapper cardMapper;

    @Override
    public PageHolder<List<FlowDTO>> listFlows(long userId, long cardId, int currentPage, int pageSize) {

        int total = flowMapper.countFlow(cardId);
        PageHolder<List<FlowDTO>> pageHolder = PageHolder.init(currentPage, total, pageSize);

        //集合中放的是数据的数据，相当于是原材料，我们需要再加工才能返回给客户端
        List<FlowDomain> flows = flowMapper.listFlow(cardId, pageHolder.offset(), pageSize);
        //使用集合的时候，如果知道具体的个数，那么就应该在构造的时候，给个初始容量，防止扩容。
        List<FlowDTO> dtos = new ArrayList(flows.size());

        for (FlowDomain flow : flows) {
            FlowDTO flowDTO = new FlowDTO();
            dtos.add(flowDTO);

            flowDTO.setAmount(MoneyUtil.fenToYuan(flow.getAmount()));
            flowDTO.setCardNum(CardUtil.formatNum(flow.getCardNum()));
            flowDTO.setCreateTime(DateUtil.date2String(flow.getCreateTime()));
            flowDTO.setRemark(flow.getRemark());
        }

        pageHolder.setData(dtos);

        return pageHolder;
    }

    @Override
    public List<FlowDTO> listTop10(long userId) {
        List<FlowDomain> flowDomains = flowMapper.listTop10(userId);
        if (flowDomains.isEmpty()) {
            return Collections.EMPTY_LIST;
        }

        List<FlowDTO> dtos = new ArrayList<>(flowDomains.size());
        for (FlowDomain flow : flowDomains) {
            FlowDTO flowDTO = new FlowDTO();
            dtos.add(flowDTO);

            flowDTO.setRemark(flow.getRemark());
            flowDTO.setCreateTime(DateUtil.date2String(flow.getCreateTime()));
            flowDTO.setCardNum(CardUtil.formatNum(flow.getCardNum()));
            flowDTO.setAmount(MoneyUtil.fenToYuan(flow.getAmount()));
        }

        return dtos;
    }


    /**
     * 查询流水 会有时间的限制 比如最近一周 一个月 半年等
     * @param cardId
     * @param userId
     * @return
     */
    @Override
    public List<FlowDTO> listCardFlow(long cardId, long userId) {
        Card card = cardMapper.selectByPrimaryKey(cardId);
        if (null == card) {
            throw new BizException("银行卡不存在");
        }

        if (userId != card.getUserId()) {
            throw new BizException("无权操作");
        }

        //假如此卡有10万比流水 你这sql是不是就有问题了  可以采用分页的方式查询
        List<FlowDomain> flowDomains = flowMapper.listCardFlow(cardId);
        if (flowDomains.isEmpty()) {
            return Collections.EMPTY_LIST;
        }

        List<FlowDTO> dtos = new ArrayList<>(flowDomains.size());
        for (FlowDomain flow : flowDomains) {
            FlowDTO flowDTO = new FlowDTO();
            dtos.add(flowDTO);

            flowDTO.setAmount(MoneyUtil.fenToYuan(flow.getAmount()));
            flowDTO.setCardNum(flow.getCardNum());
            flowDTO.setRemark(flow.getRemark());
            flowDTO.setCreateTime(DateUtil.date2String(flow.getCreateTime()));
        }

        return dtos;
    }


    @Override
    public CardFlowStatisticsDTO loadCardStatisticData(long userId, long cardId) {
        List<CardFlowDomain> cardFlowDomains = flowMapper.statisticCardFlow(cardId);
        //收入
        int income = 0;
        //支出的
        int outcome = 0;
        for (CardFlowDomain cardFlowDomain : cardFlowDomains) {
            if (cardFlowDomain.getTotalAmount() < 0) {
                //防止溢出 int(最大值21亿)
                outcome += cardFlowDomain.getTotalAmount();
            } else {
                income += cardFlowDomain.getTotalAmount();
            }
        }

        Card card = cardMapper.getById(cardId);

        CardFlowStatisticsDTO dto = new CardFlowStatisticsDTO();
        dto.setCardId(cardId);
        dto.setCardNum(card.getCardNum());
        dto.setExpenditureAmount(MoneyUtil.fenToYuan(Math.abs(outcome)));
        dto.setIncomeAmount(MoneyUtil.fenToYuan(income));
        return dto;
    }
}
